package edu.yuhf.streamdemo;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Stream;
import java.util.function.Supplier;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;

public class CollectOperation {
    public void collectExample(){
        // 创建示例数据流
        Stream<String> stream = Stream.of("apple", "banana", "cherry", "date", "elderberry");
        // 使用三个参数的collect方法将新元素添加到列表中
        List<String> result = stream.collect(new Supplier<List<String>>() {
             public List<String> get() {
                 var list = new ArrayList<String>();
                 list.add("test1");
                 list.add("test2");
                 return list;
             }}, new BiConsumer<List<String>, String>() {
                 public void accept(List<String> list, String element) {
                     list.add(element);
                 }
             }, new BiConsumer<List<String>, List<String>>() {
                 public void accept(List<String> list1, List<String> list2) {
                     list1.addAll(list2);
                 }
             }
        );
//        List<String> result = stream.collect(()->{
//                    var list = new ArrayList<String>();
//                    list.add("test1");
//                    list.add("test2");
//                    return list;
//                },ArrayList::add,ArrayList::addAll);

        System.out.println("Collected result: " + result);
        System.out.println("Result type: " + result.getClass().getName());
    }
}
